<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="操作类型" v-bind="validateInfos.defectStatusOptType">
	          <j-dict-select-tag v-model:value="formData.defectStatusOptType" dictCode="defect_status_opt_type" placeholder="请选择操作类型" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="当前状态" v-bind="validateInfos.currentStatus">
	          <j-dict-select-tag v-model:value="formData.currentStatus" dictCode="bug_status" placeholder="请选择当前状态" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="当前状态" v-bind="validateInfos.currentStatusContent">
            <a-input v-model:value="formData.currentStatusContent" placeholder="请输入当前状态" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="变更状态" v-bind="validateInfos.changeStatus">
	          <j-dict-select-tag v-model:value="formData.changeStatus" dictCode="bug_status" placeholder="请选择变更状态" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="变更状态" v-bind="validateInfos.changeStatusContent">
            <a-input v-model:value="formData.changeStatusContent" placeholder="请输入变更状态" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="修改人" v-bind="validateInfos.changeOwner">
			  <j-select-user v-model:value="formData.changeOwner" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="修改人角色" v-bind="validateInfos.changeOwnerRole">
            <a-input v-model:value="formData.changeOwnerRole" placeholder="请输入修改人角色" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="指向人" v-bind="validateInfos.nextOwner">
			  <j-select-user v-model:value="formData.nextOwner" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="指向人角色" v-bind="validateInfos.nextOwnerRole">
            <a-input v-model:value="formData.nextOwnerRole" placeholder="请输入指向人角色" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="修改时间" v-bind="validateInfos.changeTime">
		        <a-date-picker placeholder="请选择修改时间"  v-model:value="formData.changeTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="修改备注" v-bind="validateInfos.changeComment">
            <a-input v-model:value="formData.changeComment" placeholder="请输入修改备注" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否重新打开" v-bind="validateInfos.isReopen">
            <a-input v-model:value="formData.isReopen" placeholder="请输入是否重新打开" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="重新打开" v-bind="validateInfos.reopenContent">
            <a-input v-model:value="formData.reopenContent" placeholder="请输入重新打开" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="重新打开次数" v-bind="validateInfos.reopenTimes">
	          <a-input-number v-model:value="formData.reopenTimes" placeholder="请输入重新打开次数" style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="影响范围" v-bind="validateInfos.infects">
            <a-input v-model:value="formData.infects" placeholder="请输入影响范围" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="不解决原因" v-bind="validateInfos.noSloveReason">
            <a-input v-model:value="formData.noSloveReason" placeholder="请输入不解决原因" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="缺陷原因" v-bind="validateInfos.defectReason">
            <a-input v-model:value="formData.defectReason" placeholder="请输入缺陷原因" :disabled="disabled" allowClear></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../CrowdDefectStatusRecord.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    defectStatusOptType: '',   
    currentStatus: '',   
    currentStatusContent: '',   
    changeStatus: '',   
    changeStatusContent: '',   
    changeOwner: '',   
    changeOwnerRole: '',   
    nextOwner: '',   
    nextOwnerRole: '',   
    changeTime: '',   
    changeComment: '',   
    isReopen: '',   
    reopenContent: '',   
    reopenTimes: undefined,
    infects: '',   
    noSloveReason: '',   
    defectReason: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
